package com.hdoit.apollo.web.shop.apis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.shop.model.Address;
import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.hdoit.apollo.logical.viewmodel.shop.CustomerSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/api/v1/customer/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/customer")
public class CustomerApi extends BaseApi{

	@Autowired
	private ICustomerService customerService;
	
	private final String saveCustomerApi="saveCustomer";
	private final String getCustomers="getCustomers";
	private final String getCustomersByScroll="getCustomersByScroll";
	private final String getCustomerListByCargoIdApi="getCustomerListByCargoId";
	private final String getCustomerByCustomerIdApi="getCustomerByCustomerId";
	private final String getCustomerById="getCustomerById";
	private final String getRetailCustomer="getRetailCustomer";
	
	private final String queryCustomerInfoByCustomerIdApi="queryCustomerInfoByCustomerId";
	
    @RequestMapping(value = saveCustomerApi)
    @ApiOperation(notes = saveCustomerApi, httpMethod = "POST", value = "保存客户信息")
    public @ResponseBody String saveCustomer(Customer customer,String addressesJson){
    	Customer customerPan = customerService.queryModel(customer.getCustomerId());
    	if(customerPan==null){
    		customer.setCreateDate(DateUtils.getCurrent());
    		customer.setCreateUserId(getLoginId());
            customer.setCreateUserName(getLoginName());
            customer.setDeleteMark(YesOrNoInteger.no);
            customer.setModelStatus(ModelStatus.add);
    	}else{
    		customer.setModifyUserId(getLoginId());
    		customer.setModifyUserName(getLoginName());
    		customer.setModifyDate(DateUtils.getCurrent());
    		customer.setModelStatus(ModelStatus.update);
    		customer.setCustomerDebt(customerPan.getCustomerDebt());
    		customer.setPaymentMoney(customerPan.getPaymentMoney());
    	}
    	
    	
    	if(addressesJson!=null&&addressesJson.isEmpty()){
    		System.err.println(addressesJson+"/////////////////////");
    		@SuppressWarnings("unchecked")
        	List<Address> addresses=(List<Address>) JsonUtils.readValues(addressesJson, List.class, Address.class);
    		ResponseModel rm=new ResponseModel();
        	if(!customerService.applyChange(customer,addresses)){
        		rm.setStatus(ResponseStatus.dataError);
        	}
        	return rm.toJson();
    	}else {
    		ResponseModel rm=new ResponseModel();
    		if(!customerService.applyChange(customer)){
        		rm.setStatus(ResponseStatus.dataError);
        	}
    		return rm.toJson();
		}
 	
    }
    
    @RequestMapping(value = getRetailCustomer)
    @ApiOperation(notes = getRetailCustomer, httpMethod = "GET", value = "通过获取店面零客户")
    public @ResponseBody String getRetailCustomer(String customerId,String shopId){
    	Customer customer = customerService.getRetailCustomer(shopId);
    	ResponseModel rm=new ResponseModel();
    	rm.put("customer",customer);
    	return rm.toJson();
    }
    
    @RequestMapping(value = getCustomerById)
    @ApiOperation(notes = getCustomerById, httpMethod = "GET", value = "通过Id获取客户列表")
    public @ResponseBody String getCustomerById(String customerId,String shopId){
    	Customer customer = customerService.getCustomerById(customerId,shopId);
    	ResponseModel rm=new ResponseModel();
    	rm.put("customer",customer);
    	return rm.toJson();
    }

    @RequestMapping(value = getCustomers)
    @ApiOperation(notes = getCustomers, httpMethod = "GET", value = "获取客户列表")
    public @ResponseBody String getGroupInfo(CustomerSearch search){
    	List<Customer> customers = customerService.queryListMP(search,null);
    	ResponseModel rm=new ResponseModel();
    	rm.put("customers",customers);
    	return rm.toJson();
    }
    
 	@RequestMapping(value = getCustomersByScroll)
    @ApiOperation(notes = getCustomersByScroll, httpMethod = "POST", value = "滚动查找获取客户列表")
    public @ResponseBody String getStocksByScrollApi(CustomerSearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}
    	List<Customer> customers = customerService.queryListMP(search,pagerModel);
    	rm.put("customers",customers);
    	return rm.toJson();
    }
    
    @RequestMapping(value = getCustomerListByCargoIdApi)
    @ApiOperation(notes = getCustomerListByCargoIdApi, httpMethod = "GET", value = "获取欠货订单客户列表")
    public @ResponseBody String getCustomerListByCargoId(String cargoId){
    	List<Customer> customersForCargo = customerService.getCustomerListByCargoId(cargoId);
    	ResponseModel rm=new ResponseModel();
    	rm.put("customersForCargo",customersForCargo);
    	return rm.toJson();
    }
    
    @RequestMapping(value = getCustomerByCustomerIdApi)
    @ApiOperation(notes = getCustomerByCustomerIdApi, httpMethod = "GET", value = "获取欠货订单客户列表")
    public @ResponseBody String getCustomerByCustomerId(String customerId){
    	Customer customer = customerService.getCustomerByCustomerId(customerId);
    	ResponseModel rm=new ResponseModel();
    	rm.put("customer",customer);
    	return rm.toJson();
    }
    
    @RequestMapping(value = queryCustomerInfoByCustomerIdApi)
    @ApiOperation(notes = queryCustomerInfoByCustomerIdApi, httpMethod = "GET", value = "获取编辑客户详细信息")
    public @ResponseBody String queryCustomerInfoByCustomerId(String customerId){
    	Customer customer = customerService.queryCustomerInfoByCustomerId(customerId);
    	ResponseModel rm=new ResponseModel();
    	rm.put("customer",customer);
    	return rm.toJson();
    }

}
